<!DOCTYPE html>
<html>
  <head>
    <title>列表页</title>
    <link rel='stylesheet' href='/css/bootstrap.css' />
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-xs-12 col-lg-8 col-lg-offset-2">
          <div class="row text-right">
            <div class="col-xs-12">
              <a href="/logout" class="btn btn-danger">退出登录</a>
            </div>
          </div>
          <div class="row">
            <h2 class="col-xs-6">记账本</h2>
            <div class="col-xs-6 text-right"><a href="/account/create" class="btn btn-primary">添加账单</a></div>
          </div>
          
          <hr>
          <div class="accounts">
            <!-- 列表渲染 -->
            <% accounts.forEach(item=>{ %>
            <div class="panel <% item.type===-1?'panel-danger':'panel-success' %>">
              <div class="panel-heading"><%= moment(item.time).format('YYYY-MM-DD') %></div>
              <div class="panel-body">
                <div class="col-xs-6"><%= item.title %></div>
                <div class="col-xs-2 text-center">
                  <span class="label <% item.type===-1?'label-warning':'label-success' %>"><% item.type===-1?'支出':'收入' %></span>
                </div>
                <div class="col-xs-2 text-right"><%= item.account %>元</div>
                <div class="col-xs-2 text-right">
                  <!-- <a href="/account/<%= item.id %>"> -->
                  <!-- 绑定事件优先于跳转连接 -->
                  <a class="delBtn" href="/account/<%= item._id %>">
                    <span class="glyphicon glyphicon-remove">X</span>
                  </a>
                </div>
              </div>
            </div>
            <% }) %>
          </div>
        </div>
      </div>
    </div>
  </body>
  <script>
    //获取所有delBtn
    let delBtn = document.querySelectorAll('.delBtn');
    //绑定事件
    delBtn.forEach(item=>{
      item.addEventListener('click',function(e){
        if(confirm('您确定要删除吗？')){
          return true;//如果点击是直接放行
        }else{
          //阻止默认行为
          e.preventDefault();
        }
      })
    })
  </script>
</html>
